<?php

namespace app\sports_admin\service;

use app\exception\AdminException;
use think\Exception;
use think\Request;
use app\sports_admin\model\Login as LoginModel;
use app\sports_admin\model\LoginLog as LoginLogModel;

class Login
{
    /**
     * 登录验证
     * @param $username 用户名
     * @param $pwd 密码
     * @param $code 验证码
    */
    public static function checkLogin()
    {
        $params  = Request::instance()->only(['username','pwd']);
        self::adminLoginLogInsert(trim($params['username']));
        return self::checkAdminInfo(trim($params['username']),trim($params['pwd']));
    }
    /**
     * 验证用户信息
     * @param $username 用户名
     * @param $password 密码
    */
    public static function checkAdminInfo($username,$password)
    {
        $adminInfo = LoginModel::findAdmin($username);
        if(!$adminInfo){
            throw new AdminException([
                'msg'=>'账号不存在',
                'errorCode'=>'404'
            ]);
        }
        $adminInfo = $adminInfo->toArray();
        if($password != authcode($adminInfo['pwd'],'DECODE',$adminInfo['login'],0)){
            throw new AdminException([
                'msg'=>'账号或密码错误',
                'errorCode'=>'400'
            ]);
        }
        $scope = self::getAdminRule($adminInfo['id']);
        $adminInfo['scope'] = $scope;
        return $adminInfo;
    }
    /**
     * 获取管理员权限
     * @param $adminId 管理员id
    */
    public static function getAdminRule($adminId)
    {
        $rule = LoginModel::getAdminRule($adminId);
        return $rule->admin_rule->get_rule->scope;
    }
    /**
     * 写入管理员登录信息
     * @param $account 用户名
    */
    public static function adminLoginLogInsert($account)
    {
        $params = Request::instance();
        //获取ip
        $input['client_ip'] = sprintf('%u',ip2long($params->ip()));
        $input['account'] = $account;
        //获取浏览器类型
        $input['browser'] = self::getBrowser();
        $bool = LoginLogModel::adminLoginLogInsert($input);
        if(!$bool){
            throw new AdminException([
                'msg'=>'登录日志写入失败',
                'errorCode'=>400
            ]);
        }
    }
    //获取浏览器类型
    public static function getBrowser()
    {
        $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "Maxthon(Microsoft IE)";
        } elseif (strpos($user_OSagent, "Maxthon 2.0")) {
            $visitor_browser = "Maxthon 2.0";
        } elseif (strpos($user_OSagent, "Maxthon")) {
            $visitor_browser = "Maxthon";
        } elseif (strpos($user_OSagent, "Edge")) {
            $visitor_browser = "Edge";
        } elseif (strpos($user_OSagent, "Trident")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "MSIE 较高版本";
        } elseif (strpos($user_OSagent, "NetCaptor")) {
            $visitor_browser = "NetCaptor";
        } elseif (strpos($user_OSagent, "Netscape")) {
            $visitor_browser = "Netscape";
        } elseif (strpos($user_OSagent, "Chrome")) {
            $visitor_browser = "Chrome";
        } elseif (strpos($user_OSagent, "Lynx")) {
            $visitor_browser = "Lynx";
        } elseif (strpos($user_OSagent, "Opera")) {
            $visitor_browser = "Opera";
        } elseif (strpos($user_OSagent, "MicroMessenger")) {
            $visitor_browser = "微信浏览器";
        } elseif (strpos($user_OSagent, "Konqueror")) {
            $visitor_browser = "Konqueror";
        } elseif (strpos($user_OSagent, "Mozilla/5.0")) {
            $visitor_browser = "Mozilla";
        } elseif (strpos($user_OSagent, "Firefox")) {
            $visitor_browser = "Firefox";
        } elseif (strpos($user_OSagent, "U")) {
            $visitor_browser = "Firefox";
        } else {
            $visitor_browser = "其它";
        }
        return $visitor_browser;
    }
}